package ucap.com.lesanapp.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

import java.io.File;

import ucap.com.lesanapp.R;
import ucap.com.lesanapp.gesture.GestureImageView;
import ucap.com.lesanapp.md5.MD5Utils;
import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * Created by wekingwang on 16/10/25.
 */

public class ItemFragment extends Fragment {

    public PhotoViewAttacher photoViewAttacher;
    public int height;//屏幕高
    public int width;//屏幕宽

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.item_fragemnt, null);
        final PhotoView photoView = (PhotoView) view.findViewById(R.id.photo_view);
        Bundle arguments = getArguments();
        final String uri = arguments.getString("uri");
        HttpUtils utils = new HttpUtils();
        Display dp = getActivity().getWindowManager().getDefaultDisplay();
        width = dp.getWidth();
        height = dp.getHeight();
        Log.i("display", width + "w--h" + height);
        final String imagePath = "/sdcard/image/" + MD5Utils.encryption(uri) + ".png";
        utils.download(uri, imagePath, new RequestCallBack<File>() {
            @Override
            public void onSuccess(ResponseInfo<File> responseInfo) {
                Bitmap bitmap = BitmapFactory.decodeFile(responseInfo.result.getPath());
                Log.i("display", bitmap.getWidth() + "image_w---image_h" + bitmap.getHeight());
                if (bitmap.getWidth() > width || bitmap.getHeight() > height) {
                    setImage(responseInfo.result, photoView, 1500, 2500);
                } else {
                    setImage(responseInfo.result, photoView, 0, 0);
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
                Log.i("display", bitmap.getWidth() + "image_w---image_h" + bitmap.getHeight());
                if (bitmap.getWidth() > width || bitmap.getHeight() > height) {
                    setImage(new File(imagePath), photoView, 1500, 2500);

                } else {
                    setImage(new File(imagePath), photoView, 0, 0);
                }
            }
        });
        return view;
    }

    private void setImage(File file, ImageView imageView, int wight, int hight) {

        if (hight != 0 && wight != 0) {
            //由于图片过大，直接调用系统图片浏览器
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(file), "image/*");
            startActivity(intent);
            getActivity().finish();
        } else {
            Glide.with(this)
                    .load(file)
                    .into(imageView);
        }
        photoViewAttacher = new PhotoViewAttacher(imageView);
    }

    /**
     * 刷新图片控件
     */
    public void update() {
        if (photoViewAttacher != null) {
            photoViewAttacher.update();
        }
    }
}
